package com.itheima.youdianda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.pojo.SysConfig;

import com.itheima.youdianda.pojo.SysConfigDto;
import com.itheima.youdianda.pojo.SysDict;
import com.itheima.youdianda.service.SysConfigService;
import com.itheima.youdianda.service.SysDictService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sys_config")
@Slf4j
public class SysConfigController {
    @Autowired
    private SysConfigService service;

    //添加配置
    @PostMapping("add")
    public R<String> add(@RequestBody SysConfig sysConfig) {
        log.info("{}", sysConfig.toString());
        return service.add(sysConfig);

    }

    @GetMapping
    public void m() {
        System.out.println("1");
    }

   /* //返回所有配置数据
    @GetMapping("all")
    public R<List<SysConfig>> allConfig(){
        List<SysConfig> list = service.list();
        return R.success(list);
    }*/


    //返回所有配置数据
    @GetMapping("all")
    public R allConfig() {
        List<SysConfig> list1 = service.list();
        List<SysConfigDto> dtoList = SysConfigDto.sysConfigList(list1);
        //根据配置种类来返回
        //获取配置类对应的table值base,
        List<String> typelist = typelist();
        //根据对应的类型来返回相应的配置
        List<HashMap> list = list(typelist);


        //allData数据
        List<HashMap> alldata = allDat(dtoList);
        //configForm配置形式
        HashMap<String, String> configForm = configForm(dtoList);
        //configResult
        HashMap<String, String> configResult = configResult(dtoList);


        data a = new data();
        a.setList(list);
        a.setAllData(alldata);
        a.setConfigForm(configForm);
        a.setConfigRules(configResult);


        return R.success(a);
    }

    private HashMap<String, String> configResult(List<SysConfigDto> list1) {
        HashMap<String, String> configRules = new HashMap<>();
        String attrName = "站点名称";
        for (SysConfigDto config : list1) {
            if (config.getAttr_name().equals(attrName)) {
                configRules.put(config.getAttr_key(), config.getValidator());
                break;
            }
        }
        return configRules;
    }

    private HashMap<String, String> configForm(List<SysConfigDto> list1) {
        HashMap<String, String> map = new HashMap<>();
        list1.stream().map(item -> {
            map.put(item.getAttr_key(), item.getAttr_value());
            return item;
        }).collect(Collectors.toList());
        return map;
    }

    private List<HashMap> allDat(List<SysConfigDto> list1) {

        List<HashMap> alldataList = list1.stream().map(item -> {
            HashMap map = new HashMap();
            map.put("id", item.getId());
            map.put("attr_key", item.getAttr_key());
            map.put("attr_value", item.getAttr_value());
            map.put("validator", item.getValidator());
            map.put("config_value", item.getConfig_value());
            map.put("type", item.getType());

            return map;
        }).collect(Collectors.toList());
        return alldataList;
    }

    //传来的是custom,base,system
    private List<HashMap> list(List<String> typelist) {
        List<HashMap> collect = typelist.stream().map(typeid -> {
            //对应的集合
            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysConfig::getTabValue, typeid);
            List<SysConfig> grouplist = service.list(wrapper);
            //转移到dtolist
            List<SysConfigDto> dtoList = SysConfigDto.sysConfigList(grouplist);
            //收集小组分类集合
            List<String> groups = dtoList.stream().map(type -> {
                return type.getGroup_value();
            }).distinct().collect(Collectors.toList());
            HashMap groupmap = new HashMap();
            //各个小组的对应系统配置
            List<HashMap> mapList = groups.stream().map(group -> {
                //小组的系统配置只出现一次
                LambdaQueryWrapper<SysConfig> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SysConfig::getGroupValue, group);
                List<SysConfig> list = service.list(wrapper1);
                List<SysConfigDto> dtoList1 = SysConfigDto.sysConfigList(list);
                //封装一层
                HashMap map = new HashMap();
                map.put("data", dtoList1);
                map.put("name", group);
                return map;
            }).collect(Collectors.toList());
            //在外面小组所有的配置
            groupmap.put("children", mapList);
            if (typeid.equals("base")){
                groupmap.put("name", "基础配置");
            }
            if (typeid.equals("custom")){
                groupmap.put("name", "其他设置");
            }
            if (typeid.equals("system")){
                groupmap.put("name", "系统配置");
            }

            groupmap.put("key", typeid);


            //测试能添加数据吗；
            return groupmap;
        }).collect(Collectors.toList());


        return collect;
    }

    private List<String> typelist() {
        //所有的配置类
        List<SysConfig> list = service.list();
        List<String> collect = list.stream().map(item -> {
            return item.getTabValue();
        }).distinct().collect(Collectors.toList());
        return collect;
    }



    @PostMapping("update")
    public R update(@RequestBody List<Map> maps){
        log.info("修改数据",maps);
        maps.stream().map(map -> {
             map.get("id");
            map.get("attr_key");
            map.get("attr_value");
            LambdaUpdateWrapper<SysConfig> wrapper=new LambdaUpdateWrapper<>();
            wrapper.eq(SysConfig::getId,map.get("id"));
            wrapper.set( SysConfig::getAttrKey,map.get("attr_key"));
            wrapper.set( SysConfig::getAttrValue,map.get("attr_value"));
            service.update(wrapper);
            return map;
        }).collect(Collectors.toList());
        return R.success("");

    }

    @Autowired
    private SysDictService sysDictService;


}

@Data
class Object {
    private String name;
    private String key;
    private Map children;
}

@Data
class data {
    private List<HashMap> list;
    private List<HashMap> allData;
    private HashMap<String, String> configForm;
    private HashMap<String, String> configRules;
}

@Data
class alldata {
    private int id;
    private String attr_key;
    private String attr_value;
    private String validator;
    private String config_value;
    private int type;
}
